// 1963. 使字符串平衡的最小交换次数
// https://leetcode.cn/problems/minimum-number-of-swaps-to-make-the-string-balanced/submissions/611915550/

class Solution {
public:
    int minSwaps(string s) {
        int n=s.size(),ans=0,res=0;
        for(int i=0;i<n;++i){
            if(s[i]=='[')
                ans++;
            else if(ans)
                ans--;
            else 
                res++;
        }
        return (res+1)/2;
    }
};